﻿using SmallJob.Data;
using SmallJob.Data.Interfaces;
using SmallJob.Model;
using SmallJob.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SmallJob.Services.Implements
{
    public class TransactionService : RepositoryBase<Transaction>, ITransactionService
    {
        public TransactionService(IDatabaseFactory DbFactory)
            : base(DbFactory)
        {
        }


        public Transaction GetTransactionForOrder(int payerid, int receiverid, int provideid)
        {
            return (from t in SmallJobDB.Transactions
                    where t.PayerID == payerid &&
                    t.ReceiverID == receiverid &&
                    t.ProvideID == provideid &&
                    t.Status == (int)ETransactionStatus.Payer_Payment_Success
                    select t).FirstOrDefault();
        }



        public List<Transaction> GetByPage(int page, int pagesize, out int total)
        {
            var list = from u in SmallJobDB.Transactions
                       orderby u.ID descending
                       select u;
            int skip = (page - 1) * pagesize;
            total = list.ToList().Count;
            return list.Skip(skip).Take(pagesize).ToList();
        }

        public List<Transaction> GetByPageFilter(int page, int pagesize, out int total, string filter, string filtertype, string filterstatus)
        {
            bool nofilter = string.IsNullOrWhiteSpace(filter);
            bool nofiltertype = (string.IsNullOrWhiteSpace(filtertype) || filtertype.Equals("-1"));
            bool nofilterstatus = (string.IsNullOrWhiteSpace(filterstatus) || filterstatus.Equals("-1"));
            int inttype = -1;
            Int32.TryParse(filtertype, out inttype);
            int intstatus = -1;
            Int32.TryParse(filterstatus, out intstatus);
            int skip = (page - 1) * pagesize;
            var listPayer = (from d in SmallJobDB.Transactions
                             join u in SmallJobDB.Users on d.PayerID equals u.ID
                             where (nofilter ||  u.ProperName.Contains(filter) ) &&
                             (nofiltertype || inttype == -1 || d.TransactionType == inttype) &&
                             (nofilterstatus || intstatus == -1 || d.Status == intstatus)
                             orderby d.ID descending
                             select d);
            var listReceiver = (from d in SmallJobDB.Transactions
                                join u in SmallJobDB.Users on d.ReceiverID equals u.ID
                                where (nofilter || u.ProperName.Contains(filter)) &&
                                (nofiltertype || inttype == -1 || d.TransactionType == inttype) &&
                                (nofilterstatus || intstatus == -1 || d.Status == intstatus)
                                orderby d.ID descending
                                select d);
            var list = new List<Transaction>();
            list.AddRange(listPayer);
            list.AddRange(listReceiver);
            total = list.Count();
            return list.Skip(skip).Take(pagesize).ToList();
        }
    }
}